/* eslint-disable */
/**
 * Created by asus on 2017/8/29.
 */

import rowTable from 'utils/table/row-table';
import crossTable from 'utils/table/cross-table';
import columnTable from 'utils/table/column-table';
import dataTable from 'utils/table/data-table';
const main = {
  //记录指标和分组的位置信息
  xyInfo: {
    rowType:"iden",
    columnType:"group"
  },
  //存放行列操作信息
  optionInfo: {},
  rowColInfo: {},
  setXyInfo: function(rowType, columnType){
    this.xyInfo = {
      rowType: rowType,
      columnType: columnType
    }
  },
  //初始化全局变量信息
  createTable: function (idenInfo,groupInfo) {
    /*this.xyInfo = xyInfo;
    if(xyInfo.rowType == "iden"){
      this.optionInfo = {
        rowInfo:idenInfo,
        colInfo:groupInfo
      }
    }else {
      this.optionInfo = {
        rowInfo:groupInfo,
        colInfo:idenInfo
      }
    }*/
    if(groupInfo && groupInfo.length > 0){
      if(this.xyInfo.rowType == "iden"){
        this.optionInfo = {
          rowInfo:idenInfo,
          colInfo:groupInfo
        }
      }else {
        this.optionInfo = {
          rowInfo:groupInfo,
          colInfo:idenInfo
        }
      }
    }else {
      this.xyInfo = {
        rowType:"group",
        columnType:"iden"
      },
        this.optionInfo = {
          rowInfo:groupInfo,
          colInfo:idenInfo
        }
    }
    this.initTable();
  },
  //刷新表格
  refresh: function (idenInfo,groupInfo) {
    if(this.xyInfo.rowType == "iden"){
      this.optionInfo = {
        rowInfo:idenInfo,
        colInfo:groupInfo
      }
    }else {
      this.optionInfo = {
        rowInfo:groupInfo,
        colInfo:idenInfo
      }
    }
    this.initTable();
  },
  //新建表格
  initTable: function () {
    var rowInfo = this.optionInfo.rowInfo;
    var colInfo = this.optionInfo.colInfo;
    //先清空四张表的内容
    // console.log(rowInfo,'rowInfo')
    // console.log(colInfo,'colInfo')
    this.clearTable();
    // if(!rowInfo || !colInfo || colInfo.length === 0 || rowInfo.length === 0){
    //   /*alert("参数错误");*/
    //   return;
    // }
    // 二维表格
    if((colInfo && colInfo.length > 0)&&(rowInfo && rowInfo.length > 0)){
      //根据json树添加左侧row_table
      var maxColumn = rowTable.getDeepth_rowTable(this.deepClone(rowInfo))
      //data对象在使用的时候，会修改其属性，为不改变元数据的属性，在使用时对数据做一次深度拷贝
      var rowResult = rowTable.treeToRow({data:this.deepClone(rowInfo),maxCol:maxColumn,table:$("#row_table")});
      //上一行代码计算了左侧表格的行数，列数，以及全部递归成一纬的数组
      // console.log(maxColumn,'maxColumn')
      // console.log(rowResult,'rowResult')
      var crossCols = rowResult.colNum; //cross_table的列数为row_table的列数
      var dataRows = rowResult.rowNum;//data_table的行数为row_table的行数
      //根据json树构建顶部column_table
      var colResult = columnTable.treeToColumn({data:this.deepClone(colInfo),table:$("#column_table")});
      var crossRows = colResult.rowNum; //cross_table的行数为column_table的行数
      var dataCols = colResult.colNum; //data_table的列数为column_table的列数
      //根据行数列数构建交叉点表格
      crossTable.createCrossTable(crossRows,crossCols,"cross_table");
      //根据行数列数构建数据表
      var idenGroupInfo = {   //构建colnum_table和row_table时保存的行列属性信息
        rowInfo:rowResult.rowInfo,
        colInfo:colResult.colInfo
      }
      this.rowColInfo = idenGroupInfo;
      //根据行列数构建数据表
      dataTable.createDataTable(dataRows,dataCols,"data_table",this.xyInfo,idenGroupInfo);
    }else if((colInfo && colInfo.length > 0)&&(!rowInfo || rowInfo.length == 0)){
      var colResult = columnTable.treeToColumn({data:this.deepClone(colInfo),table:$("#column_table")});
      var crossRows = colResult.rowNum; //cross_table的行数为column_table的行数
      var dataCols = colResult.colNum; //data_table的列数为column_table的列数
      dataTable.createDataTableNoGroups("data_table",dataCols,colResult.colInfo);
    }else {
      return;
    }
    // console.log(dataRows,'dataRows')
    // console.log(dataCols,'dataCols')
    // console.log(this.rowColInfo,'this.rowColInfo')

  },
  //清空四张表
  clearTable: function () {
    //先清空四张表的内容
    $("#column_table tbody").html("");
    $("#row_table tbody").html("");
    $("#cross_table tbody").html("");
    $("#data_table tbody").html("");
  },
  //对象深度克隆
  deepClone: function(obj){
    var result,oClass=this.isClass(obj);
    //确定result的类型
    if(oClass==="Object"){
      result={};
    }else if(oClass==="Array"){
      result=[];
    }else{
      return obj;
    }
    for(var key in obj){
      var copy=obj[key];
      if(this.isClass(copy)=="Object"){
        result[key] = this.callee(copy); //arguments.callee(copy);//递归调用
      }else if(this.isClass(copy)=="Array"){
        result[key] = this.callee(copy);//arguments.callee(copy);
      }else{
        result[key]=obj[key];
      }
    }
    return result;
  },
  callee(data) {
    var newObj;
    newObj = JSON.parse(JSON.stringify(data));
    return newObj;
  },
  /*deepClone: function(obj) {
    var newObj = {};
    newObj = JSON.parse(JSON.stringify(obj));
    return newObj;
  },*/
  //返回传递给他的任意对象的类
  isClass: function (o){
    if(o===null) return "Null";
    if(o===undefined) return "Undefined";
    return Object.prototype.toString.call(o).slice(8,-1);
  },
//交换指标和分组的位置
  exchangeXY: function (){
    //交换指标和分组位置标志
    var row_type = (this.xyInfo.rowType == "iden") ? "group" : "iden";
    var column_type = (this.xyInfo.columnType == "iden") ? "group" : "iden";
    this.xyInfo = {
      rowType:row_type,
      columnType:column_type
    }
    //交换指标和分组信息
    var temp = {};
    temp = optionInfo.colInfo;
    optionInfo.colInfo = optionInfo.rowInfo;
    optionInfo.rowInfo = temp;
    //刷新表结构
    this.initTable();
  },
  /**
   * 读取数据
   * @returns {Array}
   */
  getData: function () {
    var datas = new Array();
    var data = {};
    $("#data_table").find("tr").each(function () {
      $(this).find("td").each(function () {
        var value = $(this).text();
        var idenCode = $(this).attr("idenCode");
        var groupCode = $(this).attr("groupCode");
        data = {
          isRegionCode: '',
          value:value,
          idenCode:idenCode,
          groupCode:groupCode
        }
        datas.push(data);
      });
    });
    return datas;
  },

  /**
   * 获取行列信息
   * @returns {{}}
   */
  getRowColInfo: function () {
    return this.rowColInfo;
  }
}
export default main
